﻿using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;

namespace DotNETX.Utility
{
    public static class ProgramUtils
    {
        public static void ChangeBinPath(string dirs = "Castle;Core;Module;UI;Misc;")
        {
            AppDomain.CurrentDomain.SetData("PRIVATE_BINPATH", dirs);
            AppDomain.CurrentDomain.SetData("BINPATH_PROBE_ONLY", dirs);
            MethodInfo m = typeof(AppDomainSetup).GetMethod("UpdateContextProperty", BindingFlags.NonPublic | BindingFlags.Static);
            MethodInfo funsion = typeof(AppDomain).GetMethod("GetFusionContext", BindingFlags.NonPublic | BindingFlags.Instance);
            m.Invoke(null, new object[] { funsion.Invoke(AppDomain.CurrentDomain, null), "PRIVATE_BINPATH", dirs });
        }

        public static bool IsExistArugment(string key, string[] args)
        {
            if (args != null)
            {
                for (int i = 0; i < args.Length; i++)
                {
                    if (string.Compare(key, args[i], true) == 0)
                    {
                        return true;
                    }
                }
            }
            return false;
        }

        public static string GetArgument(string key, string[] args, string defaultValue = null)
        {
            if (args != null)
            {
                for (int i = 0; i < (args.Length - 1); i++)
                {
                    if (string.Compare(key, args[i], true) == 0)
                    {
                        return args[i + 1];
                    }
                }
            }
            return defaultValue;
        }

        public static T GetArgument<T>(string key, string[] args, T defaultValue = default(T))
            where T : IConvertible
        {
            string strVal = GetArgument(key, args);
            T result = defaultValue;
            try
            {
                result = (T)Convert.ChangeType(strVal, typeof(T));
            }
            catch
            {
            	
            }
            return result;
        }
    }
}
